<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link href="stable/static/css/site.css" rel="stylesheet" type="text/css">
    <link href="stable/static/css/print.css" rel="stylesheet" type="text/css" media="print">
    <link href="stable/static/css/prettify.css" rel="stylesheet" type="text/css">
    <link href="//www.google.com/images/icons/product/chrome-16.png" rel="icon" type="image/ico">
    <link href="stable/static/css/api.css" rel="stylesheet" type="text/css">
    <title>chrome.ttsEngine - chrome插件中文开发文档(非官方)</title>
  </head>
  <body>
    <a id="top"></a>
    <div id="header">{Header content}</div>
    <a id="gc-topnav-anchor"></a>
    <div id="gc-topnav">
      <h1>Google Chrome Extensions</h1>
      <ul id="home" class="gc-topnav-tabs">
        <li id="home_link">
          <a href="index.html" title="Google Chrome Extensions home page"><div>Home</div></a>
        </li>
        <li id="docs_link">
          <a href="docs.html" title="Official Google Chrome Extensions documentation"><div>Docs</div></a>
        </li>
        <li id="faq_link">
          <a href="faq.html" title="Answers to frequently asked questions about Google Chrome Extensions"><div>FAQ</div></a>
        </li>
        <li id="samples_link">
          <a href="samples.html" title="Sample Extensions (with source code)"><div>Samples</div></a>
        </li>
        <li id="group_link">
          <a href="http://groups.google.com/a/chromium.org/group/chromium-extensions" title="Google Chrome Extensions developer forum"><div>Group</div></a>
        </li>
        <li id="so_link">
          <a href="http://stackoverflow.com/questions/tagged/google-chrome-extension" title="[google-chrome-extension] tag on Stack Overflow"><div>Questions?</div></a>
        </li>
      </ul>
    </div>
    <div id="gc-container">
      <div id="gc-sidebar">
        <ul 
            class="level1 ">
          <li class="level2">
                <a href="getstarted.html" class="level2 ">Getting Started</a>
          </li>
          <li class="level2">
                <a href="overview.html" class="level2 ">Overview</a>
          </li>
          <li class="level2">
                <a href="whats_new.html" class="level2 ">What's New?</a>
          </li>
          <li class="level2">
                <a href="devguide.html" class="level2 ">Developer's Guide</a>
              <ul 
                  class="level2 ">
                <li class="level3">
                    <a class="button level3">
                      <span class="level3">Browser UI</span>
                      <div class="toggleIndicator level3"></div>
                    </a>
                    <ul toggleable
                        class="level3 hidden">
                      <li class="level4">
                            <a href="browserAction.html" class="level4 ">Browser Actions</a>
                      </li>
                      <li class="level4">
                            <a href="contextMenus.html" class="level4 ">Context Menus</a>
                      </li>
                      <li class="level4">
                            <a href="notifications.html" class="level4 ">Desktop Notifications</a>
                      </li>
                      <li class="level4">
                            <a href="omnibox.html" class="level4 ">Omnibox</a>
                      </li>
                      <li class="level4">
                            <a href="options.html" class="level4 ">Options Pages</a>
                      </li>
                      <li class="level4">
                            <a href="override.html" class="level4 ">Override Pages</a>
                      </li>
                      <li class="level4">
                            <a href="pageAction.html" class="level4 ">Page Actions</a>
                      </li>
                    </ul>
                </li>
                <li class="level3">
                    <a class="button level3">
                      <span class="level3">Browser Interaction</span>
                      <div class="toggleIndicator level3"></div>
                    </a>
                    <ul toggleable
                        class="level3 hidden">
                      <li class="level4">
                            <a href="bookmarks.html" class="level4 ">Bookmarks</a>
                      </li>
                      <li class="level4">
                            <a href="cookies.html" class="level4 ">Cookies</a>
                      </li>
                      <li class="level4">
                            <a href="devtools.html" class="level4 ">Developer Tools</a>
                      </li>
                      <li class="level4">
                            <a href="events.html" class="level4 ">Events</a>
                      </li>
                      <li class="level4">
                            <a href="history.html" class="level4 ">History</a>
                      </li>
                      <li class="level4">
                            <a href="management.html" class="level4 ">Management</a>
                      </li>
                      <li class="level4">
                            <a href="tabs.html" class="level4 ">Tabs</a>
                      </li>
                      <li class="level4">
                            <a href="windows.html" class="level4 ">Windows</a>
                      </li>
                    </ul>
                </li>
                <li class="level3">
                    <a class="button level3">
                      <span class="level3">Implementation</span>
                      <div class="toggleIndicator level3"></div>
                    </a>
                    <ul toggleable
                        class="level3 hidden">
                      <li class="level4">
                            <a href="a11y.html" class="level4 ">Accessibility</a>
                      </li>
                      <li class="level4">
                            <a href="event_pages.html" class="level4 ">Event Pages</a>
                      </li>
                      <li class="level4">
                            <a href="contentSecurityPolicy.html" class="level4 ">Content Security Policy</a>
                      </li>
                      <li class="level4">
                            <a href="content_scripts.html" class="level4 ">Content Scripts</a>
                      </li>
                      <li class="level4">
                            <a href="xhr.html" class="level4 ">Cross-Origin XHR</a>
                      </li>
                      <li class="level4">
                            <a href="i18n.html" class="level4 ">Internationalization</a>
                      </li>
                      <li class="level4">
                            <a href="messaging.html" class="level4 ">Message Passing</a>
                      </li>
                      <li class="level4">
                            <a href="permissions.html" class="level4 ">Optional Permissions</a>
                      </li>
                      <li class="level4">
                            <a href="npapi.html" class="level4 ">NPAPI Plugins</a>
                      </li>
                    </ul>
                </li>
                <li class="level3">
                    <a class="button level3">
                      <span class="level3">Finishing</span>
                      <div class="toggleIndicator level3"></div>
                    </a>
                    <ul toggleable
                        class="level3 hidden">
                      <li class="level4">
                            <a href="hosting.html" class="level4 ">Hosting</a>
                      </li>
                      <li class="level4">
                            <a href="external_extensions.html" class="level4 ">Other Deployment Options</a>
                      </li>
                    </ul>
                </li>
              </ul>
          </li>
          <li class="level2">
                <a href="tutorials.html" class="level2 ">Tutorials</a>
              <ul 
                  class="level2 ">
                <li class="level3">
                      <a href="tut_migration_to_manifest_v2.html" class="level3 ">Manifest V2</a>
                </li>
                <li class="level3">
                      <a href="tut_debugging.html" class="level3 ">Debugging</a>
                </li>
                <li class="level3">
                      <a href="tut_analytics.html" class="level3 ">Google Analytics</a>
                </li>
                <li class="level3">
                      <a href="tut_oauth.html" class="level3 ">OAuth</a>
                </li>
              </ul>
          </li>
          <li class="level2">
                <span class="level2">Reference</span>
              <ul 
                  class="level2 ">
                <li class="level3">
                    <a class="button level3">
                      <span class="level3">Formats</span>
                      <div class="toggleIndicator level3"></div>
                    </a>
                    <ul toggleable
                        class="level3 hidden">
                      <li class="level4">
                            <a href="manifest.html" class="level4 ">Manifest Files</a>
                      </li>
                      <li class="level4">
                            <a href="match_patterns.html" class="level4 ">Match Patterns</a>
                      </li>
                    </ul>
                </li>
                <li class="level3">
                      <a href="permission_warnings.html" class="level3 ">Permission Warnings</a>
                </li>
                <li class="level3">
                      <a href="api_index.html" class="level3 ">chrome.* APIs</a>
                </li>
                <li class="level3">
                      <a href="api_other.html" class="level3 ">Other APIs</a>
                </li>
              </ul>
          </li>
          <li class="level2">
                <span class="level2">More</span>
              <ul 
                  class="level2 ">
                <li class="level3">
                      <a href="http://code.google.com/chrome/webstore/docs/index.html" class="level3 ">Chrome Web Store</a>
                </li>
                <li class="level3">
                      <a href="http://code.google.com/chrome/apps/docs/developers_guide.html" class="level3 ">Hosted Apps</a>
                </li>
                <li class="level3">
                      <a href="themes.html" class="level3 ">Themes</a>
                </li>
              </ul>
          </li>
        </ul>
      </div>
      <div id="gc-pagecontent">
        <h1 class="page_title">chrome.ttsEngine</h1>
        
        
        
        <div id="toc">
          <h2>Contents</h2>
          <ol>
            <li>
              <a href=#overview>Overview</a>
            </li>
            <li>
              <a href=#manifest>Manifest</a>
            </li>
            <li>
              <a href=#handling_speech_events>Handling speech events</a>
            </li>
            <li>
              <a href="#apiReference">API reference: chrome.ttsEngine</a>
              <ol>
                  
                  
                  
                  <li><a href="#events">Events</a><ol>  <li><a href="#event-onSpeak">onSpeak</a></li>  <li><a href="#event-onStop">onStop</a></li></ol></li>
                  <li><a href="#samples">Sample Extensions</a></li>
              </ol>
            </li>
          </ol>
        </div>

<p id="classSummary">
Use the <code>chrome.ttsEngine</code> module to
implement a text-to-speech (TTS) engine using an extension. If your
extension registers using this API, it will receive events containing
an utterance to be spoken and other parameters when any extension or packaged
app uses the
<a href="tts.html">tts</a>
module to generate speech. Your extension can then use any available
web technology to synthesize and output the speech, and send events back
to the calling function to report the status.
</p>


<h2 id="overview">Overview</h2>

<p>An extension can register itself as a speech engine. By doing so, it
can intercept some or all calls to functions such as
<a href="tts.html#method-speak"><code>speak()</code></a> and
<a href="tts.html#method-stop"><code>stop()</code></a>
and provide an alternate implementation.
Extensions are free to use any available web technology
to provide speech, including streaming audio from a server, HTML5 audio,
Native Client, or Flash. An extension could even do something different
with the utterances, like display closed captions in a pop-up window or
send them as log messages to a remote server.</p>

<h2 id="manifest">Manifest</h2>

<p>To implement a TTS engine, an extension must
declare the "ttsEngine" permission and then declare all voices
it provides in the extension manifest, like this:</p>

<pre>{
  "name": "My TTS Engine",
  "version": "1.0",
  <b>"permissions": ["ttsEngine"],
  "tts_engine": {
    "voices": [
      {
        "voice_name": "Alice",
        "lang": "en-US",
        "gender": "female",
        "event_types": ["start", "marker", "end"]
      },
      {
        "voice_name": "Pat",
        "lang": "en-US",
        "event_types": ["end"]
      }
    ]
  },</b>
  "background": {
    "page": "background.html",
    "persistent": false
  }
}</pre>

<p>An extension can specify any number of voices.</p>

<p>The <code>voice_name</code> parameter is required. The name should be
descriptive enough that it identifies the name of the voice and the
engine used. In the unlikely event that two extensions register voices
with the same name, a client can specify the ID of the extension that
should do the synthesis.</p>

<p>The <code>gender</code> parameter is optional. If your voice corresponds
to a male or female voice, you can use this parameter to help clients
choose the most appropriate voice for their application.</p>

<p>The <code>lang</code> parameter is optional, but highly recommended.
Almost always, a voice can synthesize speech in just a single language.
When an engine supports more than one language, it can easily register a
separate voice for each language. Under rare circumstances where a single
voice can handle more than one language, it's easiest to just list two
separate voices and handle them using the same logic internally. However,
if you want to create a voice that will handle utterances in any language,
leave out the <code>lang</code> parameter from your extension's manifest.</p>

<p>Finally, the <code>event_types</code> parameter is required if the engine can
send events to update the client on the progress of speech synthesis.
At a minimum, supporting the <code>'end'</code> event type to indicate
when speech is finished is highly recommended, otherwise Chrome cannot
schedule queued utterances.</p>

<p class="note">
<strong>Note:</strong> If your TTS engine does not support
the <code>'end'</code> event type, Chrome cannot queue utterances
because it has no way of knowing when your utterance has finished. To
help mitigate this, Chrome passes an additional boolean <code>enqueue</code>
option to your engine's onSpeak handler, giving you the option of
implementing your own queueing.  This is discouraged because then
clients are unable to queue utterances that should get spoken by different
speech engines.</p>

<p>The possible event types that you can send correspond to the event types
that the <code>speak()</code> method receives:</p>

<ul>
  <li><code>'start'</code>: The engine has started speaking the utterance.
  <li><code>'word'</code>: A word boundary was reached. Use
          <code>event.charIndex</code> to determine the current speech
          position.
  <li><code>'sentence'</code>: A sentence boundary was reached. Use
          <code>event.charIndex</code> to determine the current speech
          position.
  <li><code>'marker'</code>: An SSML marker was reached. Use
          <code>event.charIndex</code> to determine the current speech
          position.
  <li><code>'end'</code>: The engine has finished speaking the utterance.
  <li><code>'error'</code>: An engine-specific error occurred and
          this utterance cannot be spoken.
          Pass more information in <code>event.errorMessage</code>.
</ul>

<p>The <code>'interrupted'</code> and <code>'cancelled'</code> events are
not sent by the speech engine; they are generated automatically by Chrome.</p>

<p>Text-to-speech clients can get the voice information from your
extension's manifest by calling
<a href="tts.html#method-getVoices">getVoices()</a>,
assuming you've registered speech event listeners as described below.</p>

<h2 id="handling_speech_events">Handling speech events</h2>

<p>To generate speech at the request of clients, your extension must
register listeners for both <code>onSpeak</code> and <code>onStop</code>,
like this:</p>

<pre>var speakListener = function(utterance, options, sendTtsEvent) {
  sendTtsEvent({'event_type': 'start', 'charIndex': 0})

  // (start speaking)

  sendTtsEvent({'event_type': 'end', 'charIndex': utterance.length})
};

var stopListener = function() {
  // (stop all speech)
};

chrome.ttsEngine.onSpeak.addListener(speakListener);
chrome.ttsEngine.onStop.addListener(stopListener);</pre>

<p class="warning">
<b>Important:</b>
If your extension does not register listeners for both
<code>onSpeak</code> and <code>onStop</code>, it will not intercept any
speech calls, regardless of what is in the manifest.</p>

<p>The decision of whether or not to send a given speech request to an
extension is based solely on whether the extension supports the given voice
parameters in its manifest and has registered listeners
for <code>onSpeak</code> and <code>onStop</code>. In other words,
there's no way for an extension to receive a speech request and
dynamically decide whether to handle it.</p>
        <h2 id="apiReference">API Reference: chrome.ttsEngine</h2>
        <div class="api_reference">
            <h3 id="events">Events</h3>
              <div>
                <h4 id="event-onSpeak">onSpeak</h4>
                <div class="summary">
                  <span class="subdued">chrome.ttsEngine.</span>onSpeak<span class="subdued">.addListener</span>(function(<span>string <span class="variable">utterance</span></span>, <span>object <span class="variable">options</span></span>)<span class="subdued"> {...}</span>);
                </div>
                <div class="description">
              <p>
                    Called when the user makes a call to tts.speak() and one of the voices from this extension's manifest is the first to match the options object.
                  </p>      <h4>Listener Parameters</h4>
                    <dl>
                      <div>  <a name="property-onSpeak-utterance"></a>  <dt>    <span class="variable">utterance</span>    <span class="property">      (                <span id="typeTemplate">string</span>      )    </span>  </dt>  <dd>    The text to speak, specified as either plain text or an SSML document. If your engine does not support SSML, you should strip out all XML markup and synthesize only the underlying text content. The value of this parameter is guaranteed to be no more than 32,768 characters. If this engine does not support speaking that many characters at a time, the utterance should be split into smaller chunks and queued internally without returning an error.  </dd></div><div>  <a name="property-onSpeak-options"></a>  <dt>    <span class="variable">options</span>    <span class="property">      (                <span id="typeTemplate">object</span>      )    </span>  </dt>  <dd>    Options specified to the tts.speak() method.  </dd>  <dd>    <dl>      <div>        <a name="property-options-voiceName"></a>        <dt>          <span class="variable">voiceName</span>          <span class="property">            (              <span class="optional">optional</span>              <span id="typeTemplate">string</span>            )          </span>        </dt>        <dd>          The name of the voice to use for synthesis.        </dd>      </div>      <div>        <a name="property-options-lang"></a>        <dt>          <span class="variable">lang</span>          <span class="property">            (              <span class="optional">optional</span>              <span id="typeTemplate">string</span>            )          </span>        </dt>        <dd>          The language to be used for synthesis, in the form <em>language</em>-<em>region</em>. Examples: 'en', 'en-US', 'en-GB', 'zh-CN'.        </dd>      </div>      <div>        <a name="property-options-gender"></a>        <dt>          <span class="variable">gender</span>          <span class="property">            (              <span class="optional">optional</span>              <span id="typeTemplate">enumerated string ["male", "female"]</span>            )          </span>        </dt>        <dd>          Gender of voice for synthesized speech.        </dd>      </div>      <div>        <a name="property-options-rate"></a>        <dt>          <span class="variable">rate</span>          <span class="property">            (              <span class="optional">optional</span>              <span id="typeTemplate">double</span>            )          </span>        </dt>        <dd>          Speaking rate relative to the default rate for this voice. 1.0 is the default rate, normally around 180 to 220 words per minute. 2.0 is twice as fast, and 0.5 is half as fast. This value is guaranteed to be between 0.1 and 10.0, inclusive. When a voice does not support this full range of rates, don't return an error. Instead, clip the rate to the range the voice supports.        </dd>      </div>      <div>        <a name="property-options-pitch"></a>        <dt>          <span class="variable">pitch</span>          <span class="property">            (              <span class="optional">optional</span>              <span id="typeTemplate">double</span>            )          </span>        </dt>        <dd>          Speaking pitch between 0 and 2 inclusive, with 0 being lowest and 2 being highest. 1.0 corresponds to this voice's default pitch.        </dd>      </div>      <div>        <a name="property-options-volume"></a>        <dt>          <span class="variable">volume</span>          <span class="property">            (              <span class="optional">optional</span>              <span id="typeTemplate">double</span>            )          </span>        </dt>        <dd>          Speaking volume between 0 and 1 inclusive, with 0 being lowest and 1 being highest, with a default of 1.0.        </dd>      </div>    </dl>  </dd></div>
                    </dl>
                    <h4>Callback function</h4>
                    <p>
                          The <em>sendTtsEvent</em> parameter should specify a function
                          that looks like this:
                    </p>
                    <pre>function(<a href="tts.html#type-TtsEvent">tts.TtsEvent</a> event) <span class="subdued">{...}</span>;</pre>
                    <dl>
                      <div>  <a name="property-sendTtsEvent-event"></a>  <dt>    <span class="variable">event</span>    <span class="property">      (                <span id="typeTemplate"><a href="tts.html#type-TtsEvent">tts.TtsEvent</a></span>      )    </span>  </dt>  <dd>    The event from the text-to-speech engine indicating the status of this utterance.  </dd></div>
                    </dl>
                </div>
              </div>
              <div>
                <h4 id="event-onStop">onStop</h4>
                <div class="summary">
                  <span class="subdued">chrome.ttsEngine.</span>onStop<span class="subdued">.addListener</span>(function()<span class="subdued"> {...}</span>);
                </div>
                <div class="description">
              <p>
                    Fired when a call is made to tts.stop and this extension may be in the middle of speaking. If an extension receives a call to onStop and speech is already stopped, it should do nothing (not raise an error).
                  </p>  </div>
              </div>
        </div>
          <h2 id="samples">Sample Extensions that use chrome.ttsEngine</h2>
            <li><strong><a href="samples.html#ada4d65f29289c46d04a1920af682232">Console TTS Engine</a></strong> &ndash;
            A "silent" TTS engine that prints text to a small window rather than synthesizing speech.
        </li>
      </div>
    </div>
  </body>
  <script>
    window.bootstrap = {
      api_names: [{"name":"alarms"},{"name":"bookmarks"},{"name":"browserAction"},{"name":"browsingData"},{"name":"commands"},{"name":"contentSettings"},{"name":"contextMenus"},{"name":"cookies"},{"name":"debugger"},{"name":"declarativeWebRequest"},{"name":"devtools.inspectedWindow"},{"name":"devtools.network"},{"name":"devtools.panels"},{"name":"downloads"},{"name":"events"},{"name":"extension"},{"name":"fileBrowserHandler"},{"name":"fontSettings"},{"name":"history"},{"name":"i18n"},{"name":"idle"},{"name":"input.ime"},{"name":"management"},{"name":"omnibox"},{"name":"pageAction"},{"name":"pageCapture"},{"name":"permissions"},{"name":"privacy"},{"name":"proxy"},{"name":"runtime"},{"name":"scriptBadge"},{"name":"storage"},{"name":"tabs"},{"name":"topSites"},{"name":"tts"},{"name":"ttsEngine"},{"name":"types"},{"name":"webNavigation"},{"name":"webRequest"},{"name":"webstore"},{"last":true,"name":"windows"}].concat(
          [{"name":"experimental.bluetooth"},{"name":"experimental.devtools.audits"},{"name":"experimental.devtools.console"},{"name":"experimental.discovery"},{"name":"experimental.identity"},{"name":"experimental.infobars"},{"name":"experimental.offscreenTabs"},{"name":"experimental.processes"},{"name":"experimental.record"},{"name":"experimental.speechInput"},{"name":"experimental.systemInfo.cpu"},{"name":"experimental.systemInfo.storage"},{"last":true,"name":"experimental.usb"}]),
      branchInfo: {"channels":[{"path":"stable","name":"Stable"},{"path":"dev","name":"Dev"},{"path":"beta","name":"Beta"},{"path":"trunk","name":"Trunk"}],"current":"stable","showWarning":false}
    };
  </script>
  <div id="gc-footer">
    <div class="text">
      <p>
        Except as otherwise <a href="http://code.google.com/policies.html#restrictions">noted</a>,
        the content of this page is licensed under the <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">Creative Commons
        Attribution 3.0 License</a>, and code samples are licensed under the
        <a rel="license" href="http://code.google.com/google_bsd_license.html">BSD License</a>.
      </p>
      <p>
        ©2012 Google
      </p>
      <script src="stable/static/js/branch.js" type="text/javascript"></script>
      <script src="stable/static/js/sidebar.js" type="text/javascript"></script>
      <script src="stable/static/js/prettify.js" type="text/javascript"></script>
      <script>
        (function() {
          // Auto syntax highlight all pre tags.
          var preElements = document.getElementsByTagName('pre');
          for (var i = 0; i < preElements.length; i++)
            preElements[i].classList.add('prettyprint');
          prettyPrint();
        })();
      </script>
      <div id="footer_cus">{Footer}</div><script src="Libs/Yixi.js"></script><script src="http://s9.cnzz.com/stat.php?id=4928336&web_id=4928336" language="JavaScript"></script>
  
    </div>
  </div>
</html>
